import tinymce from 'tinymce/tinymce'
import { openResetNumberDialog } from '@/utils/exampleTag'
import { checkBeforeInsert } from '@/utils/helpers'

tinymce.PluginManager.add('itemnoteEn', function (editor) {
  const iconStr = `
    <svg width="30px" height="27.5px" viewBox="0 0 30 27.5" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
      <g id="页面-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
          <g id="编辑器页面-批注" transform="translate(-1364.000000, -78.000000)">
              <g id="tool" transform="translate(16.000000, 69.500000)">
                  <g id="编组-2" transform="translate(190.000000, 8.000000)">
                      <g id="编组-22" transform="translate(1158.000000, 0.500000)">
                          <g id="条文注" transform="translate(0.000000, 3.500000)" fill="#000000" fill-rule="nonzero">
                              <rect id="矩形" opacity="0" x="0" y="0" width="24" height="24"></rect>
                              <path d="M20.625,2.625 L3.375,2.625 C2.96015625,2.625 2.625,2.96015625 2.625,3.375 L2.625,20.625 C2.625,21.0398437 2.96015625,21.375 3.375,21.375 L20.625,21.375 C21.0398437,21.375 21.375,21.0398437 21.375,20.625 L21.375,3.375 C21.375,2.96015625 21.0398437,2.625 20.625,2.625 Z M19.6875,19.6875 L4.3125,19.6875 L4.3125,4.3125 L19.6875,4.3125 L19.6875,19.6875 Z" id="形状"></path>
                              <polygon id="路径" points="7.640625 8.1796875 7.640625 6.9609375 16.359375 6.9609375 16.359375 8.1796875 12.9375 8.1796875 12.9375 17.0390625 11.015625 17.0390625 11.015625 8.1796875"></polygon>
                          </g>
                          <g id="编组-18" transform="translate(16.000000, 0.000000)">
                              <rect id="矩形" fill="#E3E3E3" x="0" y="0" width="14" height="10" rx="1.4"></rect>
                              <polygon id="路径" fill="#000000" fill-rule="nonzero" points="3.2305 2.09383673 3.2305 7.09183673 6.9405 7.09183673 6.9405 6.44083673 3.9935 6.44083673 3.9935 4.85183673 6.6535 4.85183673 6.6535 4.20083673 3.9935 4.20083673 3.9935 2.74483673 6.8215 2.74483673 6.8215 2.09383673"></polygon>
                              <path d="M9.4815,3.37483673 C9.2575,3.37483673 9.0545,3.41683673 8.8725,3.51483673 C8.6905,3.60583673 8.5295,3.73883673 8.3895,3.92083673 L8.3895,3.47283673 L7.6475,3.47283673 L7.6475,7.09183673 L8.3895,7.09183673 L8.3895,4.91483673 C8.4175,4.62083673 8.5225,4.38983673 8.6905,4.22183673 C8.8445,4.06783673 9.0335,3.99783673 9.2435,3.99783673 C9.8035,3.99783673 10.0905,4.30583673 10.0905,4.92183673 L10.0905,7.09183673 L10.8325,7.09183673 L10.8325,4.85883673 C10.8325,3.86483673 10.3775,3.37483673 9.4815,3.37483673 Z" id="路径" fill="#000000" fill-rule="nonzero"></path>
                          </g>
                      </g>
                  </g>
              </g>
          </g>
      </g>
    </svg>
  `
  editor.ui.registry.addIcon('itemnoteIconEn', iconStr);
  return (
    editor.ui.registry.addMenuButton('itemnoteEn', {
      icon: 'itemnoteIconEn',
      tooltip: '英文条文注',
      fetch: function (callback) {
        callback([
          {
            type: 'menuitem',
            text: '英文条文注',
            onAction: function () {
              if (!checkBeforeInsert('item_note')) return
              editor.execCommand('toggleExample', false, { action: 'item_note', dataLang: 'en' })
            }
          },
          {
            type: 'menuitem',
            text: '英文条文注X',
            onAction: function () {
              if (!checkBeforeInsert('item_note')) return
              editor.execCommand('toggleExample', false, { action: 'item_note', numbered: true, dataLang: 'en' })
            }
          },
          {
            type: 'menuitem',
            text: '重新编号',
            onAction: function () {
              openResetNumberDialog(editor, 'item_note')
            }
          }
        ])
      }
    }),
    {
      getMetadata: function () {
        return {
          name: '条文注',
          url: 'http://sfrj.cn'
        }
      }
    }
  )
})
